package com.osgc.rf.unit.base;

import java.sql.Connection;

import com.inca.np.auth.Userruninfo;
import com.inca.np.gui.control.DBTableModel;
import com.inca.np.util.SelectHelper;
import com.osgc.rf.np.url.dao.DataConnect;
import com.osgc.rf.np.url.dao.Navigation;
import com.osgc.rf.np.url.dao.User;
import com.osgc.rf.np.url.util.Base;
import com.osgc.rf.unit.dao.ResultInfo;

public abstract class NewTaskModel extends Base {

	protected Navigation navigation = null;
	protected User user;
	protected Userruninfo userinfo;
	public void setInfoFileVm(String vm) {
		sessionHelper.putContext("infofilevm", vm);
	}



	abstract protected String getTemp();
	abstract protected String query(Connection con,int line) throws Exception;
	abstract protected String query(Connection con) throws Exception;
	abstract protected void setInfo(DBTableModel db,int totallines,int line);
	abstract protected ResultInfo excute(Connection con) throws Exception ;
	public void show(){
		Connection con = null;
		try {
			String index = params.get("index");
			int line = Integer.parseInt(index) - 1;
			con = DataConnect.getConnect();
			query(con,line);
			con.commit();
		} catch (Exception e) {
			logger.error(e.getMessage());
			DataConnect.rollback(con);
		} finally {
			DataConnect.close(con);
		}	
	}
	@Override
	public void beforeInvoke() throws Exception {
		navigation = (Navigation) sessionHelper.getContext("navigation");
		user = (User) sessionHelper.getContext("user");
		if (user != null) {
			userinfo = new Userruninfo();
			userinfo.setUserid(user.getUserid());
			userinfo.setUsername(user.getUsername());
			if (navigation == null) {
				navigation = new Navigation();
				sessionHelper.putContext("navigation", navigation);
			}
			navigation.setMenu(user.getOp());
		}
		setRedirect(getTemp());
	}

	@Override
	public void afterInvoke() throws Exception {

	}
	public void index() {
		navigation.setId("");
		sessionHelper.remove();
		setInfoFileVm("scan");
	}
	public void query() {		
		Connection con = null;
		try {
			con = DataConnect.getConnect();
			String barcode = query(con);
			navigation.setId(barcode);
			con.commit();
		} catch (Exception e) {
			logger.error(e.getMessage());
			DataConnect.rollback(con);
		} finally {
			DataConnect.close(con);
		}		
	}	
	public void excute() {
		Connection con = null;
		try {
			con = DataConnect.getConnect();
			ResultInfo result = excute(con);
			sessionHelper.putContext("result", result);
			con.commit();
		} catch (Exception e) {
			DataConnect.rollback(con);
			logger.error(e.getMessage());
			ResultInfo	result = ResultInfo.getErrorResult(e);
			sessionHelper.putContext("result", result);
		} finally {
			DataConnect.close(con);
		}
		setRedirect("default/json");
	}

	
	protected DBTableModel getInfo(Connection con,String sql, int line) throws Exception{	
		logger.debug(sql);
		return new SelectHelper(sql).executeSelect(con, line, 1);
	}
	protected DBTableModel getInfo(Connection con,String sql) throws Exception{	
		logger.debug(sql);
		return new SelectHelper(sql).executeSelect(con, 0, 9999);
	}
	protected int getTotallines(Connection con,String sql)throws Exception{
		int totallines = 0;
		sql ="select Count(1) from ("+sql+")";
		logger.debug(sql);
		DBTableModel dbcount = new SelectHelper(sql).executeSelect(con, 0, 1);
		if(dbcount.getRowCount()>0){
			totallines =  Integer.parseInt(dbcount.getItemValue(0, 0));
		}
		return totallines;
	}
}
